﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Linq;

namespace Perpus
{
    public partial class admin_tambahAnggota : Form
    {
        string captchacode;
        public admin_tambahAnggota()
        {
            InitializeComponent();
            captchacode = generateRandomCaptcha(4);
            pictureBox1.Image = GetImage(100, 60, captchacode);
        }


        private void button2_Click(object sender, EventArgs e)
        {
            TampilanAnggota menu = new TampilanAnggota();
            menu.Show();
            this.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NhibernateHelper help = new NhibernateHelper();
            var factory = help.CreateSessionFactory();

            using (var session = factory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var member = new Anggota
                    {
                        // kode_buku = int.Parse(textBox1.Text),
                       nama_anggota = textBox2.Text,
                       alamat_anggota = richTextBox1.Text,
                       no_telp = textBox5.Text,
                       gender_anggota = comboBox1.SelectedItem.ToString() //comboBox1.SelectedText.ToString()
                        //Name = txtCategoryName.Text,
                        //Description = txtCategoryDescription.Text
                    };
                    //session.Save(member);
                    //transaction.Commit();
                    //MessageBox.Show("Berhasil!");

                    if (textBox2.TextLength > 0 && richTextBox1.TextLength > 0 && textBox5.TextLength > 0)
                    {
                        if (textBox3.Text.Equals(captchacode))
                        {
                            session.Save(member);
                            transaction.Commit();
                            MessageBox.Show("Berhasil!");
                        }
                        else
                        {
                            MessageBox.Show("Wrong Captcha");
                        }
                    }
                   
                    //this.kategoriTableAdapter.Fill(this.cobaDataSet.Kategori);
                }
            } 
        }        
        public string generateRandomCaptcha(int noofchar)
        {
            string s = "";
            Random r = new Random(65);

            for (int i = 1; i <= noofchar; i++)
            {
                int num = r.Next(0, System.DateTime.Now.Second + 50);
                if (num >= 65 && num <= 90)
                {
                    s = s + ((char)num).ToString();
                }
                else
                    i--;
            }
            return s;
        }
        private Bitmap GetImage(int width, int height, String text)
        {
            // Create a new 32-bit bitmap image.

            Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            // Create a graphics object for drawing.

            Graphics g = Graphics.FromImage(bitmap);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            Rectangle rect = new Rectangle(0, 0, width, height);

            // Fill in the background.

            HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White);
            g.FillRectangle(hatchBrush, rect);

            // Set up the text font.

            SizeF size;
            float fontSize = rect.Height + 25;
            Font font;

            // Adjust the font size until the text fits within the image.
            do
            {
                fontSize--;
                font = new Font("TAHOMA", fontSize, FontStyle.Bold);
                size = g.MeasureString(text, font);
            }
            while (size.Width > rect.Width);
            // Set up the text format.

            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;

            // Create a path using the text and warp it randomly.

            GraphicsPath path = new GraphicsPath();
            path.AddString(text, font.FontFamily, (int)font.Style, font.Size, rect, format);
            float v = 4F;
            PointF[] points = { new PointF(new Random().Next(rect.Width) / v, new Random().Next(rect.Height) / v), new PointF(rect.Width - new Random().Next(rect.Width) / v, new Random().Next(rect.Height) / v), new PointF(new Random().Next(rect.Width) / v, rect.Height - new Random().Next(rect.Height) / v), new PointF(rect.Width - new Random().Next(rect.Width) / v, rect.Height - new Random().Next(rect.Height) / v) };
            Matrix matrix = new Matrix();
            matrix.Translate(0F, 0F);
            path.Warp(points, rect, matrix, WarpMode.Perspective, 0F);

            // Draw the text.

            hatchBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.LightGray, Color.DarkGray);
            g.FillPath(hatchBrush, path);

            // Add some random noise.

            int m = Math.Max(rect.Width, rect.Height);
            for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++)
            {
                int x = new Random().Next(rect.Width);
                int y = new Random().Next(rect.Height);
                int w = new Random().Next(m / 50);
                int h = new Random().Next(m / 50);
                g.FillEllipse(hatchBrush, x, y, w, h);
            }

            // Clean up.

            font.Dispose();
            hatchBrush.Dispose();
            g.Dispose();

            // return the bitmap image.

            return bitmap;
        }
    }
}
